//
//  SingleViewController.m
//  PickView
//
//  Created by student on 16/2/23.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "SingleViewController.h"

@interface SingleViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickview;
@property (weak, nonatomic) IBOutlet UILabel *infolabel;
@property (strong ,nonatomic) NSArray *list;
@end

@implementation SingleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list = @[@"张三",@"李四",@"王五",@"赵六",@"乔布斯",@"李1",@"王2",@"赵3",@"张4",@"李5",@"王6",@"赵7",@"张8",@"李9"];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(UIButton *)sender
{
    long int sel = [self.pickview selectedRowInComponent:0];
    NSString *name = self.list[sel];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择了谁" message:name preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                          handler:^(UIAlertAction * action) {}];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    NSLog(@"%s",__func__);
    return 1;// 当前就1列
}

// 第几列有几行?
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    NSLog(@"%s",__func__);
    return self.list.count;// 当前就一列,不需要判断传入的component,直接返回数组的数量
}

// 告诉pickView第几行是什么内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView
                      titleForRow:(NSInteger)row
                     forComponent:(NSInteger)component
{
    NSLog(@"%s 告诉pickerView的内容:%@",__func__,self.list[row]);
    return self.list[row];
}

// 选择pickView内容的时候触发的事件
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.infolabel.text = self.list[row];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
